﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Linq;
using System.Text;
using JKF.BusinessLayer.BLL;
using JKF.DataModule;
using JKF.DataModule.SqlServerDAL;
using JKF.Entities.Model;
using JKF.Substructure;

namespace JKF.BusinessLayer.BLL
{
    /// <summary>
    /// 业务逻辑类bllFEEDBACK 的摘要说明。
    /// </summary>
    public partial class bllFEEDBACK
    {
        public DataTable GetFeedBacks(string title, string status, string type, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            dalFEEDBACK dalFeedBack = new dalFEEDBACK();
            string user_key = bllSYS_USER.CurrentUser.USER_KEY.ToString();
            return dalFeedBack.GetFeedBacks(title, status,type, user_key, pageCurrent, pageSize, ref pageCount, ref rowCount);
        }

        /// <summary>
        /// 最新公告
        /// </summary>
        /// <returns></returns>
        public DataSet GetLatestFeedback()
        {/*
            var strSql = @"SELECT top 6 FEEDBACK_KEY,TITLE,FEEDBACKER,IS_REPLIED=CASE  [IS_REPLIED] WHEN '0' THEN '未回复' else '已回复' END,USER_KEY
             FROM [FEEDBACK]
             WHERE user=@user_key and type='1' AND CONVERT(CHAR(10),FEEDBACK_DATE,120)>=CONVERT(CHAR(10),GETDATE(),120)
             ORDER BY FEEDBACK_DATE DESC";
            */
            var strSql = @"SELECT top 6 FEEDBACK_KEY,TITLE,FEEDBACKER
             FROM [FEEDBACK]
             WHERE type='1' AND CONVERT(CHAR(10),FEEDBACK_DATE,120)>=CONVERT(CHAR(10),GETDATE(),120)
             ORDER BY FEEDBACK_DATE DESC";
            var dbClass = DB.HR;
            var cmd = dbClass.GetSqlStringCommand(strSql);
            dbClass.AddInParameter(cmd, "user_key", DbType.Guid, bllSYS_USER.CurrentUser.USER_KEY);
            var ds = dbClass.ExecuteDataSet(cmd);
            return ds;
        }

        public bool DeleteFeedback(string feedBackKey)
        {
            var dbClass = DB.HR;
            var cmdList = new List<DbCommand>();

            try
            {
                string logContent = "";
                logContent = "删除反馈记录";
                bllSYS_LOG.SaveLog("002005001", "FEEDBACK", feedBackKey, logContent, "删除", BusinessBase.ClientIp);

                var strSql = "DELETE  FROM  FEEDBACK WHERE FEEDBACK_KEY=@FEEDBACK_KEY";

                var cmd = dbClass.GetSqlStringCommand(strSql);
                dbClass.AddInParameter(cmd, "FEEDBACK_KEY", DbType.Guid, new Guid(feedBackKey));
                object customerKey = dbClass.ExecuteNonQuery(cmd);            
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateViewTimes(string feedBackKey)
        {
            var dbClass = DB.HR;
            var cmdList = new List<DbCommand>();

            try
            {
                var strSql = "UPDATE FEEDBACK SET VIEW_TIMES=VIEW_TIMES+1  WHERE FEEDBACK_KEY=@FEEDBACK_KEY";

                var cmd = dbClass.GetSqlStringCommand(strSql);
                dbClass.AddInParameter(cmd, "FEEDBACK_KEY", DbType.Guid, new Guid(feedBackKey));
                object customerKey = dbClass.ExecuteNonQuery(cmd);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public string EditOrInsertFeedback(FEEDBACKinfo feedbackInfo)
        {
            var dal = new dalFEEDBACK();
            string FEEDBACK_KEY = feedbackInfo.FEEDBACK_KEY.ToString();
            if (!string.IsNullOrEmpty(feedbackInfo.FEEDBACK_KEY.ToString()) && FEEDBACK_KEY != "00000000-0000-0000-0000-000000000000")
            {
                var tmodule = dal.GetModel(feedbackInfo.FEEDBACK_KEY);
                if (tmodule == null) throw new Exception("数据不存在.");

                feedbackInfo.FEEDBACK_KEY = tmodule.FEEDBACK_KEY;
                feedbackInfo.DATE_UPDATE = DateTime.Now;
                feedbackInfo.TYPE = tmodule.TYPE;
                feedbackInfo.REPLIED_USER = tmodule.REPLIED_USER;
                feedbackInfo.IS_REPLIED=tmodule.IS_REPLIED;
                feedbackInfo.SHOP_CODE = tmodule.SHOP_CODE;
                feedbackInfo.DATE_CREATE = tmodule.DATE_CREATE;
                feedbackInfo.USER_CREATE = tmodule.USER_CREATE;
                feedbackInfo.VIEW_TIMES = tmodule.VIEW_TIMES;
                dal.Update(feedbackInfo);
                dal = null;
            }
            else
            {
                feedbackInfo.FEEDBACK_KEY = new Guid(DB.GetNewGuid(DB.HR));

                feedbackInfo.DATE_CREATE = DateTime.Now;
                feedbackInfo.DATE_UPDATE = DateTime.Now;
                feedbackInfo.USER_CREATE = bllSYS_USER.CurrentUser.USER_KEY;
                feedbackInfo.SHOP_CODE = bllSYS_USER.CurrentUser.SHOP_CODE;
                feedbackInfo.IS_REPLIED = "0";
                dal.Add(feedbackInfo);
                dal = null;
            }
            return feedbackInfo.FEEDBACK_KEY.ToString();
        }

        /// <summary>
        /// 回复反馈内容
        /// </summary>
        /// <param name="feedbackKey"></param>
        /// <param name="repliedContent"></param>
        /// <returns></returns>
        public bool ReplyFeedback(string feedbackKey, string repliedContent)
        {
            var dbClass = DB.HR;
            var cmdList = new List<DbCommand>();

            try
            {
                var strSql = "UPDATE FEEDBACK SET IS_REPLIED='1',REPLIED_DATE=getdate(),REPLIED_CONTENT=@REPLIED_CONTENT,REPLIED_USER=@REPLIED_USER WHERE FEEDBACK_KEY=@FEEDBACK_KEY";

                var cmd = dbClass.GetSqlStringCommand(strSql);
                dbClass.AddInParameter(cmd, "FEEDBACK_KEY", DbType.Guid, new Guid(feedbackKey));
                dbClass.AddInParameter(cmd, "REPLIED_CONTENT", DbType.String, repliedContent);
                dbClass.AddInParameter(cmd, "REPLIED_USER", DbType.Guid, bllSYS_USER.CurrentUser.USER_KEY);
                object customerKey = dbClass.ExecuteNonQuery(cmd);              
                return true;
            }
            catch
            {
                return false;
            }
        }

        public DataTable GetModules(string MODULE_NAME, string DATE_START, string DATE_END,
  int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            dalSYS_MODULE sysModule = new dalSYS_MODULE();
            return dalSYS_MODULE.GetModules(MODULE_NAME, DATE_START, DATE_END, pageCurrent, pageSize, ref pageCount, ref rowCount);
        }

        public DataRow GetFeedbackInfo(string feedbackKey)
        {
            var strSql = @"SELECT [FEEDBACK_KEY]
          ,[TITLE]
          ,[FEEDBACKER]
          ,[FEEDBACK_DATE]
          ,[CONTENT]
          ,[FILE_PATH]
          ,[IS_REPLIED]
          ,[REPLIED_DATE]
          ,[REPLIED_USER]
          ,VIEW_TIMES
          ,B.REAL_NAME AS REPLIED_NAME
          ,[REPLIED_CONTENT]
          ,B.SHOP_CODE AS REPLIED_SHOP
           FROM [FEEDBACK] A
           LEFT JOIN dbo.SYS_USER B ON A.REPLIED_USER=B.USER_KEY
           WHERE A.FEEDBACK_KEY=@FEEDBACK_KEY";
            var dbClass = DB.HR;
            var cmd = dbClass.GetSqlStringCommand(strSql);
            dbClass.AddInParameter(cmd, "FEEDBACK_KEY", DbType.Guid, feedbackKey.ToGuid());
            var dt = dbClass.ExecuteDataSet(cmd).Tables[0];
            if (dt.Rows.Count > 0)
                dt.Rows[0]["FILE_PATH"] = GetFileName(dt.Rows[0]["FILE_PATH"].ToString());
            return (dt != null && dt.Rows.Count > 0) ? dt.Rows[0] : null;
        }

        public static string GetFileName(String path)
        {
            if (string.IsNullOrEmpty(path))
                return "";

            if (path.Contains("\\"))
            {
                string[] arr = path.Split('\\');
                return "/UploadFiles/" + arr[arr.Length - 1];
            }
            else
            {
                string[] arr = path.Split('/');
                return "/UploadFiles/" + arr[arr.Length - 1];
            }
        }
    }
}
